﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using uTILLIty.ADExtractor.Core;

namespace uTILLIty.ADExtractor.CoreTests
{
	[TestClass]
	public class AdPropertyInfoTest
	{

		public TestContext TestContext { get; set; }

		[TestMethod]
		public void ParseDisplayNameWithSubProperty()
		{
			var target = AdPropertyInfo.Parse("ManagerSID=manager.objectSID");
			Assert.AreEqual("ManagerSID", target.DisplayName);
			Assert.AreEqual("manager", target.PropertyName);
			Assert.AreEqual(0, target.SingleValueIndex);
			Assert.AreEqual(AggregationType.None, target.Aggregation);
			Assert.AreEqual(1, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseLiteral()
		{
			var target = AdPropertyInfo.Parse("ManagerSID=Literal(xxx)");
			Assert.AreEqual("ManagerSID", target.DisplayName);
			Assert.AreEqual("xxx", target.PropertyName);
			Assert.AreEqual(0, target.SingleValueIndex);
			Assert.AreEqual(AggregationType.Literal, target.Aggregation);
			Assert.AreEqual(0, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseSubPropertyWithIndex()
		{
			var target = AdPropertyInfo.Parse("memberof[2].name");
			Assert.AreEqual("memberof", target.DisplayName);
			Assert.AreEqual("memberof", target.PropertyName);
			Assert.AreEqual(2, target.SingleValueIndex);
			Assert.AreEqual(AggregationType.None, target.Aggregation);
			Assert.AreEqual(1, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseDisplayNameWithAggregationAndSubProperty()
		{
			var target = AdPropertyInfo.Parse("members=All(member.objectSID)");
			Assert.AreEqual("members", target.DisplayName);
			Assert.AreEqual("member", target.PropertyName);
			Assert.AreEqual(0, target.SingleValueIndex);
			Assert.AreEqual(AggregationType.All, target.Aggregation);
			Assert.AreEqual(1, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseValueIdxExplicit()
		{
			var target = AdPropertyInfo.Parse("manager[3]");
			Assert.AreEqual("manager", target.DisplayName);
			Assert.AreEqual("manager", target.PropertyName);
			Assert.AreEqual(3, target.SingleValueIndex);
			Assert.AreEqual(0, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseAllPossibilities()
		{
			var target = AdPropertyInfo.Parse("Street=All(manager.street)");
			Assert.AreEqual("Street", target.DisplayName);
			Assert.AreEqual("manager", target.PropertyName);
			Assert.AreEqual(AggregationType.All, target.Aggregation);
			Assert.AreEqual(0, target.SingleValueIndex);
			Assert.AreEqual(1, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseAggregation()
		{
			var target = AdPropertyInfo.Parse("count(manager)");
			Assert.AreEqual("manager", target.DisplayName);
			Assert.AreEqual("manager", target.PropertyName);
			Assert.AreEqual(0, target.SingleValueIndex);
			Assert.AreEqual(AggregationType.Count, target.Aggregation);
			Assert.AreEqual(0, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseDisplayNameWithAggregation()
		{
			var target = AdPropertyInfo.Parse("Managers:count(manager)");
			Assert.AreEqual("Managers", target.DisplayName);
			Assert.AreEqual("manager", target.PropertyName);
			Assert.AreEqual(0, target.SingleValueIndex);
			Assert.AreEqual(AggregationType.Count, target.Aggregation);
			Assert.AreEqual(0, target.SubProperties.Length);
		}

		[TestMethod]
		public void ParseSimple()
		{
			var target = AdPropertyInfo.Parse("manager");
			Assert.AreEqual("manager", target.DisplayName);
			Assert.AreEqual("manager", target.PropertyName);
			Assert.AreEqual(0, target.SingleValueIndex);
			Assert.AreEqual(AggregationType.None, target.Aggregation);
			Assert.AreEqual(0, target.SubProperties.Length);
		}
	}
}